#include <head.h>

int sock_fd;

int UDP_init(in_port_t port)
{
    //创建用户数据报 套接字 SOCK_DGRAM
    sock_fd = socket(AF_INET ,SOCK_DGRAM, 0);
    if (-1 == sock_fd)
    {
        ERRLOG("套接字创建错误");
        return 0;
    }
    
    //填充服务器网络信息结构体
    struct sockaddr_in server_addr;

    memset(&server_addr, 0 , sizeof(server_addr));

    server_addr.sin_family = AF_INET;

    server_addr.sin_port = htons(port);

    server_addr.sin_addr.s_addr = inet_addr("0.0.0.0");

    if (-1 == bind(sock_fd,(struct sockaddr *)&server_addr,sizeof(server_addr)))
    {
        ERRLOG("套接字创建错误");
        close(sock_fd);
        return 0;
    }
    
    return sock_fd;
}

void UDP_close(void)
{
    close(sock_fd);
}